var BackofficeMember = {
	account  : null, 
	
	load : function () {
		
		
		this.account = getUser().Account;
		this.eventReister();
		this.createGrid();
		Ext.getDom("userName").innerHTML  =getUser().Name; 
	},
	
	eventReister : function () {
		  Ext.get("btnQuery").on('click',this.query);  //確認,新增使用者	
		  Ext.get("btnTimeLine").on('click',this.openTimeLime);  //確認,新增使用者	
	},
	
	openTimeLime : function () {
		
		var url = "/ad/html/backoffice/timeline.php";
		var width =1024;
		var height = 800;	
		
		 /*
		if (getBrowser().isIE) {
		 	  width =800;
		      height = 550;	
		} else if (getBrowser().isFF) {
			    width =1024;
		      height = 1000;	
		} else {
		 	  width =1024;
		      height = 800;	
		}
	 
	    window.showModalDialog(url,null,'dialogHeight:'+height+'pt;dialogWidth:'+width+'pt;center:yes;status=no;scroll=no');
	    */
	 
		var left = (screen.width/2) - width/2;
   	 	var top = (screen.height/2) - height/2;
        property = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes, width=' + width + ', height='+height +',left='+left+',top='+top+',screenX='+left+',screenY='+top;
		var obj = window.open(url ,"openWin", property);
	}, 
	
	query : function () {
	    var fields = {
	    		account : Ext.getDom("account").value,
				name : Ext.getDom("name").value,
				type : getRedioValue("type"),
				isNew : getRedioValue("isNew"),
				isPay : getRedioValue("isPay"),
				start : 0,
				limit : 10 
				//type : "1"//預設查詢 媒體商
	    };
	    
	    BackofficeMember.store.addListener({
			beforeload:function(store,records,options){
				store.baseParams =  fields;
				 
			}
		});
		BackofficeMember.store.load({params: fields});   
	},
	viewEdit : function (account) {
		var url = "/ad/html/backoffice/tokenList.php?account=" + account;
		var width =800;
		var  height = 600;	
	    window.showModalDialog(url,null,'dialogHeight:'+height+'pt;dialogWidth:'+800+'pt;center:yes;status=no');
		BackofficeMember.query();
	}, 
	
	createGrid : function () {
	 	 BackofficeMember.store = new Ext.data.JsonStore ({
	 	 	url:"/ad/sys/com/control/UserService.php?action=queryMenagerMemeber",
			root : "row",
			method : "GET",
			remoteSort: false,
			totalProperty: 'count',
			fields : ["account", "name", "status", "type","count","isNew"]
		});
	 
		BackofficeMember.grid =new Ext.grid.GridPanel({
			renderTo : "grid",
			store :  BackofficeMember.store,
			height: 340,
			loadMask : true,
			viewConfig : {
				forceFit : true 
			},
			columns : [ 
				new Ext.grid.RowNumberer(),
				{header : "帳號" ,dataIndex : "account", width :80,sortable : true , renderer: function (v,p,r) {
					return String.format('<b><a href="/ad/html/backoffice/backofficeMDetail.php?account={0}">{1}</a></b>',r.data.account,v);	
				}},
				{header : "姓名", dataIndex : "name",width :40, sortable : true},
				{header : "狀態", dataIndex : "status",width :30,sortable : true,
					renderer: function (value, p , record) {
						switch (value) {
							case "0": return "停用";
							case "1": return "啟用";
						}
					} 
				},
				{header : "身分", dataIndex : "type",width :30 , sortable : true,
					renderer :function (value, p, record) {
						 
						switch (value) {
							case "0": return "管理人員";
							case "1": return "媒體人員";
							case "2": return "一般人員";
						}
					}
				},
				{header : "帳務明細", width : 40, renderer: function (v,p,r) {
					return String.format('<input type="button" class="gridBtn" onclick=BackofficeMember.viewEdit("{0}")  value="檢視編輯"/>' , r.data.account);   
				}},
				{header : "剩餘點數", dataIndex : "count" , width : 50, sortable : true,
					renderer : function (value, p , record) {
						if (value < 500) {
							return "<font color=red>"+value + " 點</font>";
						} else {
							return value + " 點";
						}
						
					}
				},
				{header : "新申請廣告", dataIndex : "isNew",width :30, sortable : true,
					renderer:function (value, p,r) {
						return value > 0 ? "<font color='blue'>是</font>" : "<font color='green'>否</font>";
					}
				} ,
				{header : "廣告方案", width : 40, dataIndex : "isNew", renderer: function (v,p,r) {
					return  String.format('<input type="button"  class="gridBtn" onclick=location.href=\'/ad/html/backoffice/backofficeMAdList.php?customAccount={0}\'  value="編輯"/>', r.data.account );   	
				}}
			],
			
			bbar : new Ext.PagingToolbar({
				pageSize :10,
				store : BackofficeMember.store,
				displayInfo : true,
				displayMsg : "顯示第 {0} 筆到 {1} 筆紀錄,共 {2} 筆紀錄",
				emptyMsg : "沒有紀錄" 
			})
		});	
		
		BackofficeMember.store.load({
			params:{
				start:0,
				limit:10 
				//type : "1"//預設查詢 媒體商
			} 
		});    
	}
	
}


Ext.onReady(function (){
	BackofficeMember.load();
});